<!DOCTYPE html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="David的博客 |  David&#39;s Blog">
    <meta name="keyword" content="程序员小冰 David&#39;s Blog">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        【Docker】Docker基本入门手册命令 - David 的博客 | David&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> David Blog </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar radius">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>David</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-镜像常用命令"><span class="toc-text">Docker 镜像常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#搜索镜像"><span class="toc-text">搜索镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#下载镜像"><span class="toc-text">下载镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#列出镜像"><span class="toc-text">列出镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#删除镜像"><span class="toc-text">删除镜像</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-容器常用命令"><span class="toc-text">Docker 容器常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#新建并启动容器"><span class="toc-text">新建并启动容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#列出容器"><span class="toc-text">列出容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#停止容器"><span class="toc-text">停止容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#强制停止容器"><span class="toc-text">强制停止容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#启动已停止的容器"><span class="toc-text">启动已停止的容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#进入容器"><span class="toc-text">进入容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#删除容器"><span class="toc-text">删除容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查看启动错误日志"><span class="toc-text">查看启动错误日志</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查看容器的IP地址（172-17-0-）"><span class="toc-text">查看容器的IP地址（172.17.0.*）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#同步宿主机时间到容器"><span class="toc-text">同步宿主机时间到容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在宿主机查看docker使用cpu、内存、网络、io情况"><span class="toc-text">在宿主机查看docker使用cpu、内存、网络、io情况</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#进入docker内部的bash"><span class="toc-text">进入docker内部的bash</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-Registry"><span class="toc-text">Docker Registry</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-Registry-2-0搭建"><span class="toc-text">Docker Registry 2.0搭建</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#推送到私有仓库"><span class="toc-text">推送到私有仓库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#修改镜像标签"><span class="toc-text">修改镜像标签</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用maven构建Docker镜像"><span class="toc-text">使用maven构建Docker镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#构建镜像"><span class="toc-text">构建镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#推送镜像到私有仓库"><span class="toc-text">推送镜像到私有仓库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#修改Docker镜像存放位置"><span class="toc-text">修改Docker镜像存放位置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-compose"><span class="toc-text">Docker compose</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#安装"><span class="toc-text">安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装命令补全工具"><span class="toc-text">安装命令补全工具</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#常用命令"><span class="toc-text">常用命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编排SpringCloud微服务"><span class="toc-text">编排SpringCloud微服务</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#所使用到的工程"><span class="toc-text">所使用到的工程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#编排模式"><span class="toc-text">编排模式</span></a></li></ol></li></ol></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> David Blog </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        【Docker】Docker基本入门手册命令
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2016-08-08 11:11:11</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#Docker" title="Docker">Docker</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <h2 id="Docker-镜像常用命令"><a href="#Docker-镜像常用命令" class="headerlink" title="Docker 镜像常用命令"></a>Docker 镜像常用命令</h2><h3 id="搜索镜像"><a href="#搜索镜像" class="headerlink" title="搜索镜像"></a>搜索镜像</h3><p>docker search java</p>
<h3 id="下载镜像"><a href="#下载镜像" class="headerlink" title="下载镜像"></a>下载镜像</h3><ul>
<li>docker pull java:8</li>
<li>docker pull macro/eureka-server:0.0.1<h3 id="列出镜像"><a href="#列出镜像" class="headerlink" title="列出镜像"></a>列出镜像</h3>docker images<h3 id="删除镜像"><a href="#删除镜像" class="headerlink" title="删除镜像"></a>删除镜像</h3></li>
<li>docker rmi java</li>
<li>docker rmi -f java </li>
<li>docker rmi -f $(docker images)</li>
</ul>
<h2 id="Docker-容器常用命令"><a href="#Docker-容器常用命令" class="headerlink" title="Docker 容器常用命令"></a>Docker 容器常用命令</h2><h3 id="新建并启动容器"><a href="#新建并启动容器" class="headerlink" title="新建并启动容器"></a>新建并启动容器</h3><ul>
<li>docker run -d -p 91:80 nginx<h3 id="列出容器"><a href="#列出容器" class="headerlink" title="列出容器"></a>列出容器</h3></li>
<li>docker ps<h3 id="停止容器"><a href="#停止容器" class="headerlink" title="停止容器"></a>停止容器</h3></li>
<li>docker stop $ContainerId<h3 id="强制停止容器"><a href="#强制停止容器" class="headerlink" title="强制停止容器"></a>强制停止容器</h3></li>
<li>docker kill $ContainerId<h3 id="启动已停止的容器"><a href="#启动已停止的容器" class="headerlink" title="启动已停止的容器"></a>启动已停止的容器</h3></li>
<li>docker start $ContainerId<h3 id="进入容器"><a href="#进入容器" class="headerlink" title="进入容器"></a>进入容器</h3><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">- docker inspect <span class="params">--format</span> <span class="string">"&#123;&#123;.State.Pid&#125;&#125;"</span> $ContainerId</span><br><span class="line">- nsenter <span class="params">--target</span> <span class="string">"$pid"</span> <span class="params">--mount</span> <span class="params">--uts</span> <span class="params">--ipc</span> <span class="params">--net</span> <span class="params">--pid</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h3><ul>
<li>docker rm $ContainerId</li>
<li>docker rm -f $(docker ps -a -q)<h3 id="查看启动错误日志"><a href="#查看启动错误日志" class="headerlink" title="查看启动错误日志"></a>查看启动错误日志</h3><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">docker</span> logs <span class="variable">$ContainerIdName</span>(或者<span class="variable">$ContainerId</span>)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="查看容器的IP地址（172-17-0-）"><a href="#查看容器的IP地址（172-17-0-）" class="headerlink" title="查看容器的IP地址（172.17.0.*）"></a>查看容器的IP地址（172.17.0.*）</h3><figure class="highlight mel"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect --<span class="keyword">format</span> <span class="string">'&#123;&#123; .NetworkSettings.IPAddress &#125;&#125;'</span> $ContainerId</span><br></pre></td></tr></table></figure>

<h3 id="同步宿主机时间到容器"><a href="#同步宿主机时间到容器" class="headerlink" title="同步宿主机时间到容器"></a>同步宿主机时间到容器</h3><figure class="highlight elixir"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker cp /etc/localtime <span class="variable">$ContainerName</span><span class="symbol">:/etc/</span></span><br></pre></td></tr></table></figure>

<h3 id="在宿主机查看docker使用cpu、内存、网络、io情况"><a href="#在宿主机查看docker使用cpu、内存、网络、io情况" class="headerlink" title="在宿主机查看docker使用cpu、内存、网络、io情况"></a>在宿主机查看docker使用cpu、内存、网络、io情况</h3><ul>
<li>查看指定容器情况：docker stats $ContainerName</li>
<li>查看所有容器情况：docker stats -a<h3 id="进入docker内部的bash"><a href="#进入docker内部的bash" class="headerlink" title="进入docker内部的bash"></a>进入docker内部的bash</h3><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- docker exec -it <span class="variable">$ContainerName</span> <span class="regexp">/bin/</span>bash</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="Docker-Registry"><a href="#Docker-Registry" class="headerlink" title="Docker Registry"></a>Docker Registry</h2><h3 id="Docker-Registry-2-0搭建"><a href="#Docker-Registry-2-0搭建" class="headerlink" title="Docker Registry 2.0搭建"></a>Docker Registry 2.0搭建</h3><ul>
<li>docker run -d -p 5000:5000 –restart=always –name registry2 registry:2<h3 id="推送到私有仓库"><a href="#推送到私有仓库" class="headerlink" title="推送到私有仓库"></a>推送到私有仓库</h3></li>
<li>docker push localhost:5000/macro/eureka-server:0.0.1<h3 id="修改镜像标签"><a href="#修改镜像标签" class="headerlink" title="修改镜像标签"></a>修改镜像标签</h3></li>
<li>docker tag macro/eureka-server:0.0.1 localhost:5000/macro/eureka-server:0.0.1</li>
</ul>
<h2 id="使用maven构建Docker镜像"><a href="#使用maven构建Docker镜像" class="headerlink" title="使用maven构建Docker镜像"></a>使用maven构建Docker镜像</h2><h3 id="构建镜像"><a href="#构建镜像" class="headerlink" title="构建镜像"></a>构建镜像</h3><ul>
<li>command：mvn clean package docker:build</li>
<li>tip：<br>  Linux服务器需要开启远程api:vi /usr/lib/systemd/system/docker.service<br>  修改为：ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock   <h3 id="推送镜像到私有仓库"><a href="#推送镜像到私有仓库" class="headerlink" title="推送镜像到私有仓库"></a>推送镜像到私有仓库</h3></li>
<li>command：mvn clean package docker:build -DpushImage</li>
<li>tip：<br>  pom.xml修改<imagename>192.168.1.71:5000/macro/${project.artifactId}:${project.version}</imagename></li>
<li>tip：<br>  docker要支持http:echo ‘{ “insecure-registries”:[“39.98.190.128:5000”] }’ &gt; /etc/docker/daemon.json <h3 id="修改Docker镜像存放位置"><a href="#修改Docker镜像存放位置" class="headerlink" title="修改Docker镜像存放位置"></a>修改Docker镜像存放位置</h3></li>
</ul>
<ol>
<li>查看Docker的存放位置：docker info | grep “Docker Root Dir”（默认为/var/lib/docker）</li>
<li>关闭Docker服务：systemctl stop docker</li>
<li>移动目录到目标路径：mv /var/lib/docker /root/data/docker</li>
<li>建立软连接：ln -s /root/data/docker /var/lib/docker</li>
</ol>
<h2 id="Docker-compose"><a href="#Docker-compose" class="headerlink" title="Docker compose"></a>Docker compose</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><ol>
<li>下载地址：<a href="https://github.com/docker/compose/releases" target="_blank" rel="noopener">https://github.com/docker/compose/releases</a></li>
<li>安装地址：/usr/local/bin/docker-compose</li>
<li>设置为可执行：sudo chmod +x /usr/local/bin/docker-compose</li>
<li>测试是否安装成功：docker-compose –version</li>
</ol>
<h3 id="安装命令补全工具"><a href="#安装命令补全工具" class="headerlink" title="安装命令补全工具"></a>安装命令补全工具</h3><p>sudo curl -L <a href="https://raw.githubusercontent.com/docker/compose/1.22.0/contrib/completion/bash/docker-compose" target="_blank" rel="noopener">https://raw.githubusercontent.com/docker/compose/1.22.0/contrib/completion/bash/docker-compose</a> -o /etc/bash_completion.d/docker-compose</p>
<h3 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h3><ul>
<li>构建、创建、启动相关容器：docker-compose up</li>
<li>列出所有容器：docker-compose ps</li>
<li>删除指定服务的容器：docker-compose rm eureka</li>
<li>对容器进行动态扩容：docker-compose scale eureka=3</li>
<li>停止相关容器：docker-compose stop eureka</li>
<li>启动相关容器：docker-compose start eureka</li>
</ul>
<h3 id="编排SpringCloud微服务"><a href="#编排SpringCloud微服务" class="headerlink" title="编排SpringCloud微服务"></a>编排SpringCloud微服务</h3><h4 id="所使用到的工程"><a href="#所使用到的工程" class="headerlink" title="所使用到的工程"></a>所使用到的工程</h4><ul>
<li>eureka-server</li>
<li>hello-service</li>
<li>feign-consumer</li>
<li>api-gateway<h4 id="编排模式"><a href="#编排模式" class="headerlink" title="编排模式"></a>编排模式</h4></li>
</ul>
<ol>
<li>编排SpringCloud微服务：见eureka-server/docker-res/docker-compose.yml</li>
<li>简化SpringCloud微服务编排：见eureka-server/docker-res/docker-compose-simple.yml</li>
<li>编排高可用的注册中心：见eureka-server/docker-res/docker-compose-eureka.yml</li>
</ol>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        <li>
            <a target="_blank" href="https://twitter.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-twitter"></i>
                            </span>
            </a>
        </li>
        
        
        <li>
            <a target="_blank" href="https://www.zhihu.com/people/mcxiaobing">
                            <span class="fa-stack fa-lg">
                                 <i class="iconfont icon-zhihu"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank" href="http://weibo.com/mcxiaobing">
                            <span class="fa-stack fa-lg">
                                  <i class="iconfont icon-weibo"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank" href="https://www.facebook.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-facebook"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank"  href="https://github.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://segmentfault.com/u/mcxiaobing">思否</a></span>
        <span>/</span>
        
        <span><a href="https://juejin.im/user/57edcdb2a0bb9f0058ed8ed9">掘金</a></span>
        <span>/</span>
        
        <span><a href="https://www.jianshu.com/u/4e2015c5a972">简书</a></span>
        <span>/</span>
        
        <span><a href="https://gitee.com/mcxiaobing">码云</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
